<?php
//模型
class CoursePaikeModel extends MY_Model {
	function __construct(){
		parent::__construct();
		$this->table = 'course_paike';
	}
	//分页列表
	function get_list($arr=array(),$page=1,$size=10){		
		$where = " where 1 ";
		if(isset($arr['user_id']) && !empty($arr['user_id'])){
			//$where .= " and a.user_id='{$arr['user_id']}' ";
			//获取课程id
			$cid = '';
			$query = $this->db->query("select cid from ".$this->db->dbprefix."user_card where user_id='{$arr['user_id']}' ");
			$cid_arr = $query->result_array();			
			foreach($cid_arr as $k=>$v){
				$cid[] = $v['cid'];
			}
			if($cid){
				$cid = implode(',',$cid);
			}else{
				$cid = 0;
			}
			$where .= " and p.cid in ({$cid}) ";
		}
		
		if(isset($arr['date']) && !empty($arr['date'])){
			$where .= " and p.date in ({$date}) ";
		}
		
		$sql  = "select p.*, c.name as course_name, c.type_id from ".$this->db->dbprefix.$this->table." as p left join
				".$this->db->dbprefix."course as c 	on p.cid=c.id		
				{$where} LIMIT ".($page-1)*$size .",{$size}";				
		$query = $this->db->query($sql);
		$res = $query->result_array();		
		
		foreach($res as $k=>$v){
			if($v['type_id']==0){
				$res[$k]['type_name'] = '团教';
			}else{
				$res[$k]['type_name'] = '私教';
			}
		}
		
		$sql  = "select count(*) as num from ".$this->db->dbprefix.$this->table." as p ".$where;
		$query = $this->db->query($sql);
		$row = $query->row_array();
		return array('list'=>$res,'count' =>$row['num']);		
	}
	//全部列表
	function get_lists($arr=array()){
		$where = " where 1 ";
		if(isset($arr['user_id']) && !empty($arr['user_id'])){			
			//获取课程id
			if(isset($arr['card_id']) && !empty($arr['card_id'])){
				$query = $this->db->query("select cid from ".$this->db->dbprefix."user_card where cid='{$arr['card_id']}' ");
				$card_info = $query->row_array();
		
				$where .= " and p.cid = '{$card_info['cid']}' ";
			}else{
				$cid = '';
				$query = $this->db->query("select cid from ".$this->db->dbprefix."user_card where user_id='{$arr['user_id']}' ");
				$cid_arr = $query->result_array();			
				foreach($cid_arr as $k=>$v){
					$cid[] = $v['cid'];
				}
				if($cid){
					$cid = implode(',',$cid);
				}else{
					$cid = 0;
				}
				$where .= " and p.cid in ({$cid}) ";
			}
		}
		
		
		if(isset($arr['date']) && !empty($arr['date'])){
			$where .= " and p.sdate = '{$arr['date']}' ";
		}
		
		$sql  = "select p.*, c.name as course_name, c.type_id, m.nickname as coach_name, m.headimg as coach_head, s.name as shop_name from ".$this->db->dbprefix.$this->table." as p left join
				".$this->db->dbprefix."course as c 	on p.cid=c.id left join 
				".$this->db->dbprefix."coach as m 	on m.id=p.coach_id left join 
				".$this->db->dbprefix."shop as s 	on s.id=c.shop_id 
				
				".$where;
		$query = $this->db->query($sql);
		$res = $query->result_array();
		
		foreach($res as $k=>$v){
			if($v['type_id']==0){
				$res[$k]['type_name'] = '团教';
			}else{
				$res[$k]['type_name'] = '私教';
			}
		}
		
		return $res;		
	}
	//详情
	function get_info($id){
		$sql  = "select p.*, c.name as course_name, c.type_id, c.shop_id, c.duration from 
				".$this->db->dbprefix.$this->table." as p left join
				".$this->db->dbprefix."course as c 	on p.cid=c.id
				where p.id='{$id}' ";
		$query = $this->db->query($sql);
		$row = $query->row_array();		
		return $row;
	}
	//唯一条件获取详情
	function unique_get_info($field,$value){
		$sql  = "select * from ".$this->db->dbprefix.$this->table." where {$field}='{$value}' ";
		$query = $this->db->query($sql);
		$row = $query->row_array();		
		return $row;
	}
	
}

